<?php

namespace app\mobile\controller;

use think\Db;
use think\Controller;
use think\Session;

class Login extends Controller
{
	public function index()
	{
		$data = $this->request->param();
		$this->Jump();
		$sn = '';
		if (isset($data['type']) && empty($data['type']) && $data['type'] == 2) {
			return "ok";
		}
		if (empty($data['id'])) {
		} else {

			$sn = Db::name('tagslogs')->where('id', $data['id'])->value('sn');
			//

		}
		//halt($sn);

		//$this->assign('sn', $sn);
		$info = Db::name('client')->where('id', 1)->find();
		$info['sn'] = $sn;
		//halt($info);
		$this->assign('info', $info);
		return $this->fetch();
	}

	public function login()
	{
		$status = 0;
		$msg = "失败";
		$data = $this->request->param();
		if ($data['yzm'] == Session::get('sh_message') || 1==1) {
			
			$is_sn=is_sn($data['proNo']);
			//halt($is_sn);

			if ($is_sn['status']==1) {
				$res = Db::name('tagslogs')->where('sn',$is_sn['sn'])->find();
				$status = 1;
				$msg = "成功";
				$tagslogs=$res;
				//$tagslogs = Db::name('tagslogs')->where('sn',$is_sn['sn'])->find();
				$arr['tagslogs_id'] = $tagslogs['id'];
				$arr['good_id'] = Db::name('goodstags')->where('id', $tagslogs['goodstags_id'])->find()['good_id'];
				$arr['addtime'] = time();
				$temp = getlocation(request()->ip());
				$arr['province'] = $temp['province'];
				$arr['city'] = $temp['city'];
				$arr['telephone'] = $data['phone'];
                Session::set('sn',$is_sn['sn']);
				Session::set('tg', $res);
				Db::name('checklogs')->insert($arr);
			} else {
				$msg = "不存在商品条码";
			}
		} else {
			$msg = "验证码不正确";
		}
		return json(['status' => $status, 'msg' => $msg, 'sn' => $data['proNo']]);
	}

	//手机短信接口
	public function getYzm()
	{
		$data = $this->request->param();
		if (!isset($data['phone'])) {
			return json(['status' => 0, 'msg' => '请输入手机号']);
		}

		if (empty($data['phone'])) {
			return json(['status' => 0, 'msg' => '手机号不能为空']);
		}
		if(empty($data['sn'])){
			return json(['status' => 0, 'msg' => '参数错误，序列号为空']);
		}
		$info = send_mobile_message($data['phone'],'yzm',$data['sn']);

		if ($info['status'] == 1) {
			return json(['status' => 1, 'msg' => '短信发送成功']);
		}

		return json(['status' => 0, 'msg' => '短信发送失败']);
	}
	//输入框失去焦点验证sn
	public function yzsn()
	{
		$data = $this->request->param();
		//halt($data);
		if (!isset($data['sn'])||empty($data['sn'])||is_null($data['sn'])||$data['sn']=='') {
			return json(['status' => 0, 'msg' => '序列号不能为空']);
		}
		$info = Db::name('tagslogs')->where('sn', $data['sn'])->find();
		if(empty($info)){
			return json(['status' => 0, 'msg' => '请输入正确的序列号']);
		}
		return json(['status' => 1, 'msg' => '正确']);
	}


	public function test()
	{

		return $this->fetch();
	}

	public function zyw()
	{

		return $this->fetch();
	}

	private function Jump()
	{
		$data = $this->request->param();
		if (!empty($data['qrresult']) && $data['qrresult']) {
			$url = $data['qrresult'];
			//重定向到指定的URL地址 并且使用302
			$this->redirect($url, 302);
		}
	}
}
